<?php

namespace App\Http\Controllers\API\Manager;

use Illuminate\Http\Request;
use App\Http\Controllers\API\APIController;

use Validator;
use App\Models\API\User;
use App\Services\AliyunOSS\OSS;

class UserController extends APIController
{
    public function store(Request $request)
    {
        $v = Validator::make($request->all(), [
            'college'       => 'required|max:100|college',
            'professional'  => 'required|max:50|professional',
            'student_id'    => 'required|unique:users|student_id',
            'college_entry' => 'required|max:20|college_entry',
            'user_name'     => 'required|max:50',
            'notes'         => 'max:255'
        ]);

        if ($v->fails())
        {
            return response()->json($this->setJson(false, $v->errors()), 422);
        }

        if ($v->passes())
        {
            $userId   = $request->get('user_id');
            $userImg  = $this->upload($userId, $request->get('user_img'));
            $wrap     = $this->upload($userId, $request->get('wrap'));
            $user = User::find($userId);
            $user->user_name = $request->get('user_name');
            $user->user_img  = $userImg;
            $user->wrap      = $wrap;
            $user->college   = $request->get('college');
            $user->professional  = $request->get('professional');
            $user->student_id    = $request->get('student_id');
            $user->college_entry = $request->get('college_entry');
            $user->sex   = $request->get('sex');
            $user->state = $request->get('state');
            $user->birth = $request->get('birth');
            $user->notes = $request->get('notes');

            if ($user->save()) {
                return response()->json($this->setJson(true, '创建成功！'), 200);
            } else {
                return response()->json($this->setJson(false, '服务器错误'), 500);
            }
        }
    }

    public function show($id)
    {
        $user = User::where('id', $id)->first();

        if (count($user->user_name) === 0) {
            $msg = ['is_new_user' => true];
            return response()->json($this->setJson(true, $msg), 200);
        }

        return response()->json($this->setJson(true, $user), 200);
    }

    public function update(Request $request, $id)
    {
        $v = Validator::make($request->all(), [
            'college'       => 'required|max:100|college',
            'professional'  => 'required|max:50|professional',
            'student_id'    => 'required|student_id',
            'college_entry' => 'required|max:20|college_entry',
            'user_name'     => 'required|max:50',
            'notes'         => 'max:255'
        ]);

        if ($v->fails())
        {
            return response()->json($this->setJson(false, $v->errors()), 422);
        }

        if ($v->passes())
        {
            $user = User::find($id);
            
            $userImg  = $this->upload($id, $request->get('user_img'), $user->user_img);

            $user->user_img  = $userImg;
            $user->user_name = $request->get('user_name');
            $user->college  = $request->get('college');
            $user->professional = $request->get('professional');
            $user->student_id    = $request->get('student_id');
            $user->college_entry = $request->get('college_entry');
            $user->sex   = $request->get('sex');
            $user->state = $request->get('state');
            $user->birth = $request->get('birth');
            $user->notes = $request->get('notes');

            if ($user->save()) {
                return response()->json($this->setJson(true, '修改成功！'), 200);
            } else {
                return response()->json($this->setJson(false, '服务器错误'), 500);
            }
        }
    }

    /**
     * 规范JSON
     *
     * @return Array
     */
    private function setJson($status, $msg)
    {
        $res = [
            'status' => $status,
            'msg'    => $msg
        ];
        return $res;
    }

    /**
     * 上传图片
     *
     * @return string
     */
    private function upload($userId, $base64, $oldUrl = false)
    {
        $base64Url = explode(',', $base64);

        if (count($base64Url) === 1) {
            return $base64;
        } else {
            $img = base64_decode($base64Url[1]);
            // 文件名 userId + 秒 + 随机数 + .jpg
            $time = time();
            $rand = mt_rand(0000, 9999);
            $fileName = $userId.$time.$rand.'.jpg';
            $res = OSS::upload($fileName, $img);
            $pathName = 'http://imlooke.oss-cn-qingdao.aliyuncs.com/'.$fileName;

            if ($oldUrl != false) {
                $res = OSS::deleteObject(substr($oldUrl, -19));
            }
            return $pathName;
        }
    }

    // 头像 背景 姓名的修改
    // public function uploadStore(Request $request, $id)
    // {
    //     $v = Validator::make($request->all(), [
    //         'user_name'     => 'required|max:50'
    //     ]);

    //     if ($v->fails())
    //     {
    //         return response()->json($this->setJson(false, $v->errors()), 422);
    //     }

    //     if ($v->passes())
    //     {
    //         $user = User::find($id);
    //         $userImg  = $this->upload($id, $request->get('user_img'), $user->user_img);
    //         $wrap     = $this->upload($id, $request->get('wrap'), $user->wrap);
    //         $user->user_name = $request->get('user_name');
    //         $user->user_img  = $userImg;
    //         $user->wrap     = $wrap;
    //         if ($user->save()) {
    //             return response()->json($this->setJson(true, '创建成功！'), 200);
    //         } else {
    //             return response()->json($this->setJson(false, '服务器错误'), 500);
    //         }
    //     }
    // }
}
